package com.thread.test20180827.dieLock;

/**
 * @Description: DieLockTest 死锁测试
 * @author: conceit
 * @Date: 2018/8/27 23:06
 */
public class DieLockTest extends Thread{

    private boolean flag;

    public DieLockTest(boolean flag){
        this.flag=flag;
    }

    /**
     * @Description: run 死锁测试执行方法
     * @author: conceit
     * @Date: 2018/8/27 23:14
     */
    @Override
    public void run(){
        try {
            while (true){
                if(flag){
                    synchronized (MainDieLockTest.innerMainDieLock1){
                        System.out.print(Thread.currentThread().getName()+"持有innerMainDieLock1");
                        //让先进入的线程进入sleep状态，一边有足够的时间让后续的线程持有第二个死锁对象
                        Thread.sleep(5000);
                        synchronized (MainDieLockTest.innerMainDieLock2){
                            System.out.println(Thread.currentThread().getName()+"持有innerMainDieLock2");
                        }
                    }
                }else {
                    synchronized (MainDieLockTest.innerMainDieLock2){
                        System.out.print(Thread.currentThread().getName()+"持有innerMainDieLock2");
                        //让先进入的线程进入sleep状态，一边有足够的时间让后续的线程持有第二个死锁对象
                        Thread.sleep(5000);
                        synchronized (MainDieLockTest.innerMainDieLock1){
                            System.out.println(Thread.currentThread().getName()+"持有innerMainDieLock1");
                        }
                    }
                }

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}


